import { resolve } from "path";
import { defineConfig } from "vite";
import dts from "vite-plugin-dts";

const entryPoints = {
  "index": resolve(__dirname, 'src/index.ts'),
  "db": resolve(__dirname, 'src/db/index.ts'),
  "fs": resolve(__dirname, 'src/fs/index.ts'),
  "http": resolve(__dirname, 'src/http/index.ts'),
  "job": resolve(__dirname, 'src/job/index.ts'),
  "kv": resolve(__dirname, 'src/kv/index.ts'),
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function external(source: string, _importer: string | undefined, _isResolved: boolean): boolean {
  return source.startsWith("wasi:") || source.startsWith("trailbase:");
}

export default defineConfig({
  build: {
    outDir: "./dist",
    minify: false,
    lib: {
      entry: Object.values(entryPoints),
      formats: ["es"],
    },
    rollupOptions: {
      plugins: [
        // NOTE: Needs to be in `rollupOptions` rather than vite's plugins to apply to each input.
        dts({
          rollupTypes: true,
          // NOTE: Inlucde .d.ts files generated by `jco`.
          copyDtsFiles: true,
          // NOTE: The .d.ts files generated by `jco` contain dynamic imports.
          staticImport: true,
        }),
      ],
      input: entryPoints,
      output: {
        entryFileNames: "[name].js",
      },
      external,
    },
  },
});
